﻿Imports System.Data
Imports System.IO
Imports System.Resources

Public Class frmParamObservateur
    Public dtObs As New DataTable
    Public intIdObs1 As Nullable(Of Integer)
    Public intIdObs2 As Nullable(Of Integer)

    Private Sub frmParamObservateur_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Me.CmbObs1.Items.Count > 1 Then
            Dim arrStrCol(1) As String
            arrStrCol(0) = ""
            arrStrCol(1) = ""
            Me.CmbObs1.Items.Add(arrStrCol(UBound(arrStrCol)))
            Me.CmbObs2.Items.Add(arrStrCol(UBound(arrStrCol)))
        End If
    End Sub

    Private Sub btnRetourMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetourMenu.Click
        ClSIP.HideSIP()
        Me.Hide()
        FormMain.Show()
    End Sub

    Private Sub btnRetour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        ClSIP.HideSIP()
        Me.Hide()
        frmParamChoix.Show()
    End Sub

    Private Sub txtIdObs_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        ClSIP.ShowSIP()
    End Sub

    Private Sub Clavier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clavier.Click
        Me.txtAjoutObs.Focus()
        ClSIP.ChangeState()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Try
            OpenFileDialog1.Filter = "Fichiers texte|*.txt"
            OpenFileDialog1.ShowDialog()
            Dim sFilePath = OpenFileDialog1.FileName
            If sFilePath <> "" Then
                Cursor.Current = Cursors.WaitCursor()
                CreateDataTable(sFilePath, dtObs)
                LoadFile(sFilePath, Me.CmbObs1)
                LoadFile(sFilePath, Me.CmbObs2)
                Dim arrStrCol(1) As String
                arrStrCol(0) = ""
                arrStrCol(1) = ""
                Me.CmbObs1.Items.Add(arrStrCol(UBound(arrStrCol)))
                Me.CmbObs2.Items.Add(arrStrCol(UBound(arrStrCol)))
                Cursor.Current = Cursors.Default
                MsgBox(DirectCast(My.Resources.Images.alertListLoad, String), MsgBoxStyle.Information, "Pocket eRelevé")
            Else
                MsgBox(DirectCast(My.Resources.Images.alertFileNotSelected, String), MsgBoxStyle.Information, "Pocket eRelevé")
            End If
        Catch Excep As Exception
            Cursor.Current = Cursors.Default
            MsgBox(DirectCast(My.Resources.Images.errorOnLoadFile, String) + Excep.Message, MsgBoxStyle.Exclamation, "Pocket eRelevé")
        End Try
    End Sub

    Private Sub btnAddOneObs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddOneObs.Click
        Dim dedans As Boolean

        'Test s'il y a une valeur saisie
        'S'il n'y a rien = Non valide
        If Me.txtAjoutObs.Text = "" Then
            MsgBox(DirectCast(My.Resources.Images.alertName, String), MsgBoxStyle.Exclamation, "Pocket eRelevé")
        Else
            Try
                ' Si la liste des observateurs est vide = Création des colonnes de la dataSOurce
                If dtObs.Rows.Count = 0 Then
                    dtObs.Columns.Add("id")
                    dtObs.Columns.Add("Nom")
                End If
                'Ajout de l'observateur à la liste en cours
                'Test si le observateur est déjà dans la liste = parcourt la liste des observateurs
                dedans = False
                For x As Integer = 0 To dtObs.Rows.Count - 1
                    ' Si l'observateur existe déjà dans la liste
                    If txtAjoutObs.Text = dtObs.Rows(x).Item(1).ToString Then
                        MsgBox(DirectCast(My.Resources.Images.alertObsExist, String), MsgBoxStyle.Exclamation, "Pocket eRelevé") ' On le rejette
                        dedans = True
                        Exit Sub
                    End If
                Next
                'Ajout du observateur à la liste si celui ci n'y est pas déja (dedans)
                If dedans = False Then
                    'pour une nouvel observateur on met un id vide
                    'Ajout dans la table de données
                    Dim row As DataRow
                    row = dtObs.NewRow()
                    row.Item(0) = ""
                    row.Item(1) = Me.txtAjoutObs.Text
                    dtObs.Rows.Add(row)
                    'Ajout dans les combobox
                    CmbObs1.Items.Add(Me.txtAjoutObs.Text)
                    CmbObs2.Items.Add(Me.txtAjoutObs.Text)
                    Me.txtAjoutObs.Text = ""
                    MsgBox(DirectCast(My.Resources.Images.alertObsAdd, String))
                End If
                ClSIP.HideSIP()
            Catch ex As Exception
                MsgBox(DirectCast(My.Resources.Images.errorObsAdd, String) & ex.Message, MsgBoxStyle.Critical, "Pocket eReleve")
            End Try

        End If
    End Sub

    Private Sub CmbObs1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbObs1.SelectedIndexChanged
        Try
            'Recherche de l'identifiant de l'observateur sélectionné
            For Each dtRow As System.Data.DataRow In dtObs.Select()
                If dtRow(1).ToString = CmbObs1.Text Then
                    intIdObs1 = GetCastStringToNullableInteger(dtRow(0).ToString)
                    Exit Sub
                End If
            Next
            intIdObs1 = Nothing
        Catch ex As Exception
            MsgBox(DirectCast(My.Resources.Images.errorSelect, String) + ex.Message, MsgBoxStyle.Exclamation, "Pocket eReleve")
        End Try
    End Sub

    Private Sub CmbObs2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbObs2.SelectedIndexChanged
        Try
            'Recherche de l'identifiant de l'observateur sélectionné
            For Each dtRow As System.Data.DataRow In dtObs.Select()
                If dtRow(1).ToString = CmbObs2.Text Then
                    intIdObs2 = GetCastStringToNullableInteger(dtRow(0).ToString)
                    Exit Sub
                End If
            Next
            intIdObs2 = Nothing
        Catch ex As Exception
            MsgBox(DirectCast(My.Resources.Images.errorSelect, String) + ex.Message, MsgBoxStyle.Exclamation, "Pocket eReleve")
        End Try
    End Sub

    'Fonction qui initialise les valeurs des observateurs par les valeurs définit dans le fichier DefaultObs
    ' IN : strFilePath Chemin du fichier contenant le nom des observateurs par défaut
    Public Sub LoadDefaultObs(ByVal strFilePath As String)
        'Récupération de la liste des observateurs
        Dim line As String
        Dim arrStrCol() As String
        Dim i As Integer = 0
        Try
            Using sr As StreamReader = New StreamReader(strFilePath, System.Text.Encoding.UTF8)
                Do
                    line = sr.ReadLine()
                    If Not line Is Nothing Then
                        If i = 0 Then 'Premier observateur
                            arrStrCol = Split(line, ";")
                            'Identifiant
                            Me.intIdObs1 = GetCastStringToNullableInteger(arrStrCol(0).ToString)
                            'Nom de l'observateur = Selection d'un item dans la ComboBox
                            For j As Integer = 0 To Me.CmbObs1.Items.Count - 1
                                If Me.CmbObs1.Items(j).ToString = arrStrCol(1) Then
                                    Me.CmbObs1.SelectedIndex = j
                                    Exit For
                                End If
                            Next
                        ElseIf i = 1 Then  'Second observateur
                            arrStrCol = Split(line, ";")
                            'Identifiant
                            Me.intIdObs2 = GetCastStringToNullableInteger(arrStrCol(0).ToString)
                            'Nom de l'observateur = Selection d'un item dans la ComboBox
                            For j As Integer = 0 To Me.CmbObs2.Items.Count - 1
                                If Me.CmbObs2.Items(j).ToString = arrStrCol(1) Then
                                    Me.CmbObs2.SelectedIndex = j
                                    Exit For
                                End If
                            Next
                        End If
                    End If
                    i = i + 1
                Loop Until line Is Nothing
            End Using
        Catch ex As Exception
            MsgBox(DirectCast(My.Resources.Images.errorObsAdd, String) + " : " + ex.Message, MsgBoxStyle.Exclamation, "Pocket eReleve")
        End Try
    End Sub


End Class